<template>
  <!-- 登陆页面整体盒子 -->
  <div class="login-container">
    <!-- 登陆的盒子 -->
    <div class="login-box">
      <!-- 标题的盒子 -->
      <div class="title-box">后台登录</div>
      <!-- 登陆表单区域 -->
       <!-- 注册表单区域 -->
       <el-form  :model="loginForm" :rules="loginRules" ref="loginRef">
        <el-form-item props="username">
          <el-input placeholder="请输入用户名" v-model="loginForm.username" maxlength="10" minlength="1"></el-input>
        </el-form-item>
        <el-form-item props="password">
          <el-input type="password" placeholder="请输入密码" v-model="loginForm.password" maxlength="15" minlength="6"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="btn-login" type="primary" @click="loginFn">登录</el-button>

        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/admin'
// import axios from 'axios'
import { mapMutations } from 'vuex'
export default {
  name: 'my-login',

  data () {
    return {
      // 当前登录人员
      radio: '2',
      // 登陆表单的数据对象
      loginForm: {
        username: 'qqq',
        password: '123456'
      },
      loginRules: { // 登录表单校验规则对象
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的大小写字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是1-10的大小写字母数字', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    ...mapMutations(['updateToken', 'updateUserInfo']),
    // 登陆的点击事件
    loginFn () {
      // 登陆的时候要先做校验
      this.$refs.loginRef.validate(async valid => {
        if (valid) {
          // 校验成功
          const { data: res } = await login(this.loginForm)
          console.log(res)
          if (res.code === 0) {
            // 登陆成功
            this.$message.success(res.msg)
            this.updateToken(res.token)
            this.updateUserInfo(res.data)
            // 跳转到布局页
            this.$router.push('/home')
          } else {
            // 登陆失败
            return this.$message.error(res.msg)
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 800px;

  .login-box {
    width: 400px;
    height: 280px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .select {
      width: 230px;
      position: absolute;
      top: 45px;
      right: -28px;
      color: #380f07;
      .el-radio {
        margin-right: 10px;
      }

    }
    .el-link.el-link--info {
      color: #380f07;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>
